<template>
  <ContainerGroup>
    <Container style="margin-bottom: 10px">
      <FilterForm
        ref="filterForm"
        :elements="elements"
        noimmediate
        @submit="handleSearch"
        @reset="handleReset"
      >
        <Button
          icon="icon-daochu"
          :btn-loading="dcLoading"
          @click="handleDealExcel"
        >导出</Button>
      </FilterForm>
    </Container>
    <Container height="fill">
      <div class="wraper">
        <Table
          :columns="tableColumns"
          :loading="tableLoading"
          :data="tableData"
          :pagination="{
            total: total,
            current: currentPage,
            pageSize: pageSize,
          }"
          @paginationChange="pageChange"
          @rowSelection="handleSelection"
        />
      </div>
    </Container>
  </ContainerGroup>
</template>
<script>
import {
  getAssessmentCyclePull,
  listAllAccountingUnit
  // getExport
} from '@/api/weight'
import {
  listByPeriod,
  dataExport
} from '@/api/dataCleaning'
export default {
  data() {
    return {
      dcLoading: false,
      tableColumns: [
        {
          title: '核算单元',
          dataIndex: 'deptName'
        },
        {
          title: '工作量指标编码',
          dataIndex: 'workloadCode'
        },
        {
          title: '工作量指标名称',
          dataIndex: 'workloadName'
        },
        // {
        //   title: "科室编码",
        //   dataIndex: "deptCodeInfo",
        // },
        // {
        //   title: '开单科室',
        //   dataIndex: 'billDeptNameInfo'
        // },
        // {
        //   title: '执行科室',
        //   dataIndex: 'exeDeptNameInfo'
        // },
        {
          title: '统计项目编码',
          dataIndex: 'projectCode'
        },
        {
          title: '统计项目名称',
          dataIndex: 'projectName'
        },
        {
          title: '点数',
          dataIndex: 'pionts'
        },
        {
          title: '汇总值',
          dataIndex: 'counts'
        },
        {
          title: '分值',
          dataIndex: 'amount'
        }
      ],
      total: 0,
      pageSize: 100,
      currentPage: 1,
      tableLoading: false,
      tableData: [],
      newCondition: {},
      elements: [
        {
          key: 'cycle',
          label: '周期',
          type: 'select',
          itemLabel: 'periodPull',
          itemValue: 'periodSuffix',
          hideSelectAll: true,
          options: [],
          value: ''
        },
        {
          key: 'accountingUnit',
          label: '核算单元',
          type: 'select',
          itemLabel: 'name',
          itemValue: 'code',
          hideSelectAll: true,
          options: [],
          value: ''
        }
      ],
      selections: []
    }
  },
  created() {
    this.getSelect()
  },
  methods: {
    handleSelection(row) {
      this.selections = row
    },
    handleDealExcel() {
      // 导出
      const params = {
        period: this.newCondition?.cycle,
        deptCode: this.newCondition.accountingUnit
        // ids: this.selections.map((item) => item.id)
      }
      this.dcLoading = true
      dataExport(params).then((res) => {
        this.dcLoading = false
        this.$utils.downloadFileFun(res, '系统数据.xls')
      })
    },
    getSelect() {
      // 周期
      getAssessmentCyclePull().then((res) => {
        if (res.code === 200) {
          this.elements[0].options = res.data
          const { periodSuffix } = res.data[0]
          this.elements[0].value = periodSuffix
          localStorage.setItem(
            'cycle',
            JSON.stringify(this.elements[0].options)
          )
          this.handleSearch(this.$refs.filterForm.getParms())
        } else {
          this.$message.error(res.msg)
        }
      })
      // 核算单元
      listAllAccountingUnit().then((res) => {
        if (res.code === 200) {
          this.elements[1].options = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 查询
    handleSearch(value) {
      this.newCondition = value
      this.currentPage = 1
      // this.getSelect()
      this.getData()
    },
    // 重置
    handleReset(val) {
      this.handleSearch(val)
    },
    // 数据
    getData() {
      const params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        period: this.newCondition?.cycle,
        // id: this.newCondition?.cycle,
        deptCode: this.newCondition.accountingUnit
      }
      this.tableLoading = true
      listByPeriod(params)
        .then((res) => {
          this.tableLoading = false
          if (res.code === 200) {
            this.tableData = res.data.records
            this.total = res.data.total
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(() => {
          this.tableLoading = false
        })
    },
    // 分页
    pageChange(payload) {
      this.currentPage = payload.current
      this.pageSize = payload.pageSize
      this.getData()
    }
  }
}
</script>
<style lang="less" scoped>
.wraper {
  display: flex;
  height: 100%;
  overflow: hidden;
  position: relative;
  .upload-demo {
    .buttons {
      margin: 0px 5px;
      height: 27px;
      margin-top: -1px;
    }
  }
}
</style>
